package cn.micai.base.pdf;

import com.itextpdf.text.*;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfWriter;
import com.itextpdf.text.pdf.parser.PdfReaderContentParser;
import com.itextpdf.text.pdf.parser.SimpleTextExtractionStrategy;
import com.itextpdf.text.pdf.parser.TextExtractionStrategy;
import com.itextpdf.tool.xml.XMLWorkerHelper;
import org.apache.pdfbox.cos.COSDocument;
import org.apache.pdfbox.io.RandomAccessFile;
import org.apache.pdfbox.pdfparser.PDFParser;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.text.PDFTextStripper;
import org.apache.poi.xwpf.usermodel.BreakType;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;
import org.fit.pdfdom.PDFDomTree;

import javax.xml.parsers.ParserConfigurationException;
import java.io.*;

/**
 * 描述：PDF操作工具类
 * <p>
 *
 * @author: 赵新国
 * @date: 2018/6/29 17:05
 */
public class PdfUtils {

    /**
     * 由pdf文件转成docx
     * @param pdfFileName pdf文件路径
     * @param docxFileName docx文件路径
     * @throws IOException
     */
    public static void generateDocFromPDF(String pdfFileName, String docxFileName) throws IOException {
        XWPFDocument doc = new XWPFDocument();

        String pdf = pdfFileName;
        PdfReader reader = new PdfReader(pdf);
        PdfReaderContentParser parser = new PdfReaderContentParser(reader);

        for (int i = 1; i <= reader.getNumberOfPages(); i++) {
            TextExtractionStrategy strategy = parser.processContent(i, new SimpleTextExtractionStrategy());
            String text = strategy.getResultantText();
            XWPFParagraph p = doc.createParagraph();
            XWPFRun run = p.createRun();
            run.setText(text);
            run.addBreak(BreakType.PAGE);
        }

        FileOutputStream out = new FileOutputStream(docxFileName);
        doc.write(out);
        out.close();
        reader.close();
        doc.close();
    }

    /**
     * 由pdf文件转成txt
     * @param pdfFileName
     * @param txtFileName
     * @throws IOException
     */
    public static void generateTxtFromPDF(String pdfFileName, String txtFileName) throws IOException {
        File file = new File(pdfFileName);
        String parsedText;
        PDFParser parser = new PDFParser(new RandomAccessFile(file, "r"));
        parser.parse();

        COSDocument cosDoc = parser.getDocument();

        PDFTextStripper pdfStripper = new PDFTextStripper();
        PDDocument pdDoc = new PDDocument(cosDoc);

        parsedText = pdfStripper.getText(pdDoc);

        if (cosDoc != null) {
            cosDoc.close();
        }
        if (pdDoc != null) {
            pdDoc.close();
        }

        PrintWriter pw = new PrintWriter(txtFileName);
        pw.print(parsedText);
        pw.close();
    }

    /**
     * 由pdf文件转成html
     * @param pdfFileName
     * @param htmlFileName
     * @throws ParserConfigurationException
     * @throws IOException
     */
    public static void generateHTMLFromPDF(String pdfFileName, String htmlFileName) throws ParserConfigurationException, IOException {
        PDDocument pdf = PDDocument.load(new File(pdfFileName));
        PDFDomTree parser = new PDFDomTree();
        Writer output = new PrintWriter(htmlFileName, "utf-8");
        parser.writeText(pdf, output);
        output.close();
        if (pdf != null) {
            pdf.close();
        }
    }

    /**
     * 由txt文件转成pdf
     * @param txtFileName
     * @param pdfFileName
     * @throws IOException
     */
    public static void generatePDFFromTxt(String txtFileName, String pdfFileName) throws IOException, DocumentException {
com.itextpdf.text.Document pdfDoc = new Document(PageSize.A4);
        PdfWriter.getInstance(pdfDoc, new FileOutputStream(pdfFileName))
                .setPdfVersion(PdfWriter.PDF_VERSION_1_7);
        pdfDoc.open();

        Font myfont = new Font();
        myfont.setStyle(Font.NORMAL);
        myfont.setSize(11);
        pdfDoc.add(new Paragraph("\n"));

        BufferedReader br = new BufferedReader(new FileReader(txtFileName));
        String strLine;
        while ((strLine = br.readLine()) != null) {
            Paragraph para = new Paragraph(strLine + "\n", myfont);
            para.setAlignment(Element.ALIGN_JUSTIFIED);
            pdfDoc.add(para);
        }

        pdfDoc.close();
        br.close();
    }

    /**
     * 由html文件转成pdf
     * @param htmlFileName
     * @param pdfFileName
     * @throws IOException
     * @throws DocumentException
     */
    public static void generatePDFFromHTML(String htmlFileName, String pdfFileName) throws IOException, DocumentException {
        Document document = new Document();
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(pdfFileName));
        document.open();
        XMLWorkerHelper.getInstance().parseXHtml(writer, document, new FileInputStream(htmlFileName));
        document.close();
    }

}
